package com.bw.kotlinim.ui.activity

import com.bw.kotlinim.R
import com.bw.kotlinim.base.BaseActivity
import com.bw.kotlinim.contract.register.RegisterContract
import com.bw.kotlinim.contract.register.RegisterPresenter
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

class RegisterActivity : BaseActivity(), RegisterContract.View {
    private val presenter = RegisterPresenter(this)
    override fun init() {
        super.init()
        register.setOnClickListener { register() }
        password.setOnEditorActionListener{_,_,_->
            register()
            true
        }
    }

    private fun register(){
        hideSoftKeyboard()
        val userNameString = userName.text.toString().trim()
        val passwordString = password.text.toString().trim()
        val confirmPasswordString = confirmPassword.text.toString().trim()

        presenter.register(userNameString,passwordString,confirmPasswordString)
    }
    override fun getLayoutId(): Int = R.layout.activity_register

    override fun onUserNameError() {
        userName.error = getString(R.string.user_name_error)
    }

    override fun onPasswordError() {
        password.error = getString(R.string.password_error)
    }

    override fun onConfirmPasswordError() {
        confirmPassword.error = getString(R.string.confirm_password_error)
    }

    override fun onStartRegister() {
        showProgress(getString(R.string.registering))
    }

    override fun onRegisterSuccess() {
        toast("注册成功")
        dismissProgress()
        finish()
    }

    override fun onRegisterFailed(message:String?) {
        dismissProgress()
        toast(R.string.register_failed)

    }

    override fun onUserExist() {
        dismissProgress()
        toast(R.string.user_already_exist)
    }
}